﻿using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace ServerSideTest.StoredProceduresTest
{


	public partial class StoredProceduresTest
	{
		private void IsNoRecord()
		{
			Assert.AreEqual<int>(0, Control.GetLiveInfoCount());
			Assert.AreEqual<int>(0, Control.GetTagsCount());
			Assert.AreEqual<int>(0, Control.GetBindCount());
		}

		internal SqlCommand CreateAddTagsCommand()
		{
			SqlCommand cmd = Connection.CreateCommand();
			cmd.CommandTimeout = 0;

			cmd.CommandText = "EXECUTE dbo.AddTag @sid,@cstr,@tag;";
			cmd.Parameters.Add("@sid", System.Data.SqlDbType.NVarChar);
			cmd.Parameters.Add("@cstr", System.Data.SqlDbType.NVarChar);
			cmd.Parameters.Add("@tag", System.Data.SqlDbType.NVarChar);

			ResetParameter(cmd);
			return cmd;
		}



		internal SqlCommand CreateModifyCommand()
		{
			SqlCommand cmd = Connection.CreateCommand();
			cmd.CommandTimeout = 0;
			cmd.CommandText = "EXECUTE dbo.ModifyTag @sid,@cstr,@from,@to;";
			cmd.Parameters.Add("@sid", System.Data.SqlDbType.NVarChar);
			cmd.Parameters.Add("@cstr", System.Data.SqlDbType.NVarChar);
			cmd.Parameters.Add("@from", System.Data.SqlDbType.NVarChar);
			cmd.Parameters.Add("@to", System.Data.SqlDbType.NVarChar);

			ResetParameter(cmd);
			return cmd;
		}

		internal SqlCommand CreateDeleteCommand()
		{
			SqlCommand cmd = Connection.CreateCommand();
			cmd.CommandTimeout = 0;
			cmd.CommandText = "EXECUTE dbo.DeleteTag @sid,@cstr,@tag;";
			cmd.Parameters.Add("@sid", System.Data.SqlDbType.NVarChar);
			cmd.Parameters.Add("@cstr", System.Data.SqlDbType.NVarChar);
			cmd.Parameters.Add("@tag", System.Data.SqlDbType.NVarChar);

			ResetParameter(cmd);
			return cmd;
		}

		internal SqlCommand CretateGetTag()
		{
			SqlCommand cmd = Connection.CreateCommand();
			cmd.CommandTimeout = 0;
			cmd.CommandText = "SELECT TagValue FROM dbo.GetTagValues(@sid,@cstr);";
			cmd.Parameters.Add("@sid", System.Data.SqlDbType.NVarChar);
			cmd.Parameters.Add("@cstr", System.Data.SqlDbType.NVarChar);

			ResetParameter(cmd);
			return cmd;
		}

		internal void Add(string StationId, string Caster, string TagValue)
		{
			using (SqlCommand cmd = CreateAddTagsCommand())
			{
				cmd.CommandTimeout = 0;
				if (StationId != null)
				{
					cmd.Parameters[0].Value = StationId;
				}

				if (Caster != null)
				{
					cmd.Parameters[1].Value = Caster;
				}

				if (TagValue != null)
				{
					cmd.Parameters[2].Value = TagValue;
				}

				cmd.ExecuteNonQuery();
			}
		}


		internal void Modify(string StationId, string Caster, string FromTag, string ToTag)
		{
			using (SqlCommand cmd = CreateModifyCommand())
			{
				cmd.CommandTimeout = 0;

				if (StationId != null)
				{
					cmd.Parameters[0].Value = StationId;
				}

				if (Caster != null)
				{
					cmd.Parameters[1].Value = Caster;
				}

				if (FromTag != null)
				{
					cmd.Parameters[2].Value = FromTag;
				}

				if (ToTag != null)
				{
					cmd.Parameters[3].Value = ToTag;
				}

				cmd.ExecuteNonQuery();
			}
		}


		internal void Delete(string StationId, string Caster, string TagValue)
		{
			using (SqlCommand cmd = CreateDeleteCommand())
			{
				cmd.CommandTimeout = 0;

				if (StationId != null)
				{
					cmd.Parameters[0].Value = StationId;
				}

				if (Caster != null)
				{
					cmd.Parameters[1].Value = Caster;
				}

				if (TagValue != null)
				{
					cmd.Parameters[2].Value = TagValue;
				}

				cmd.ExecuteNonQuery();
			}
		}




		internal IList<string> GetTags(string StationId, string Caster)
		{
			using (SqlCommand cmd = CretateGetTag())
			{
				if (StationId != null)
				{
					cmd.Parameters[0].Value = StationId;
				}

				if (Caster != null)
				{
					cmd.Parameters[1].Value = Caster;
				}

				List<string> Result = new List<string>();

				SqlDataReader rdr = cmd.ExecuteReader();

				while (rdr.Read())
				{
					Result.Add((string)rdr[0]);
				}

				rdr.Close();
				rdr.Dispose();


				return Result;
			}
		}



		internal static string GetCaster()
		{
			return string.Format("Caster_{0}", IdSeed++);
		}

		internal static string GetStationId()
		{
			return string.Format("Co_{0}", IdSeed++);
		}

		internal static string GetTag()
		{
			return string.Format("Tags_{0}", IdSeed++);
		}

		internal static void ResetParameter(SqlCommand Command)
		{
			for (int i = 0; i < Command.Parameters.Count; i++)
			{
				Command.Parameters[i].Value = DBNull.Value;
			}
		}

	}
}